﻿Imports System.ComponentModel
Imports DevExpress.LookAndFeel
Imports DevExpress.XtraBars
Imports System.Linq
Imports DevExpress.XtraNavBar

Public Class FMain
    Sub New()
        InitSkins()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub InitSkins()
        DevExpress.Skins.SkinManager.EnableFormSkins()
        DevExpress.UserSkins.BonusSkins.Register()
        UserLookAndFeel.Default.SetSkinStyle("Metropolis")
    End Sub

    Private Sub FMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FLogin.Close()
        InitSkins()
        Me.Text = GlobalVariables.EmpresaDesc & " | SamecaGest"
        Dim treeTesouraria As SamecaGestV2.ucTesourariaTree = New ucTesourariaTree
        treeTesouraria.Dock = DockStyle.Fill

        NavBarGroupControlContainer1.Controls.Add(treeTesouraria)

        Dim treeAdmin As SamecaGestV2.ucAdmin = New ucAdmin
        treeAdmin.Dock = DockStyle.Fill

        NavBarGroupControlContainer3.Controls.Add(treeAdmin)

        Dim treeTesourariaFornecedores As SamecaGestV2.ucTesourariaFornecedoresTree = New ucTesourariaFornecedoresTree
        treeTesourariaFornecedores.Dock = DockStyle.Fill

        NavBarGroupControlContainer2.Controls.Add(treeTesourariaFornecedores)

        Dim treeVendas As SamecaGestV2.ucVendas = New ucVendas
        treeVendas.Dock = DockStyle.Fill

        NavBarGroupControlContainer4.Controls.Add(treeVendas)

        Dim db As New MenuLinqDataContext

        Dim empresa = (From c In db.tblUserEMpresas _
                       Join o In db.tblEmpresas On c.empresa Equals o.Empresa _
                     Where c.username = GlobalVariables.UserName _
                     Select c, o)

        For Each emp In empresa

            Dim subMenuFile As BarButtonItem = New BarButtonItem(BarManager1, emp.o.EmpresaDesc)
            subMenuFile.Name = emp.o.Empresa
            If subMenuFile.Name.ToString = samecapq.Name.ToString Then
                samecapq.Enabled = True
            ElseIf subMenuFile.Name.ToString = prosam.Name.ToString Then
                prosam.Enabled = True
            ElseIf subMenuFile.Name.ToString = samecatme.Name.ToString Then
                samecatme.Enabled = True
            ElseIf subMenuFile.Name.ToString = samecaig.Name.ToString Then
                samecaig.Enabled = True
            End If
            'BarSubItem4.AddItems(New BarItem() {subMenuFile})
            'subMenuFile.Name = emp.o.Empresa

        Next

        Dim dbmain As New LinqMainDataContext

        Dim LinkForm = (From c In dbmain.tblUsers _
                               Where c.UserName = GlobalVariables.UserName _
                               Select c.DashBoard).SingleOrDefault
        If LinkForm <> "" Then
            Dim FullTypeName As String = Application.ProductName & "." & LinkForm.ToString
            Dim FormInstanceType As Type = Type.GetType(FullTypeName, True, True)
            Dim objForm As Form = CType(Activator.CreateInstance(FormInstanceType), Form)

            objForm.MdiParent = Me
            objForm.Show()

        End If

        Dim group = (From c In db.tblUserGrpMap1s _
                  Where c.UserId = GlobalVariables.UserName _
                  Select c.GroupCode).SingleOrDefault

        Dim menu = (From c In db.tblMenuLists Join d In db.tblGroupDesigns On d.MenuName Equals c.MenuName _
                      Where d.GroupCode = group _
                      Select c)
        For Each nome In menu
            If IsDBNull(nome.BarGroup) = False Then
                NavBarControl1.Groups(nome.BarGroup).Visible = True
            End If

        Next

    End Sub

    Private Sub Empresa_ItemClick(sender As Object, e As ItemClickEventArgs) Handles samecatme.ItemClick, samecapq.ItemClick, prosam.ItemClick, samecaig.ItemClick
        GlobalVariables.Empresa = e.Item.Name.ToString
        funcoes.MudarEmpresa()

    End Sub

    Private Sub BarSubItem3_ItemClick(sender As Object, e As ItemClickEventArgs) Handles BarSubItem3.ItemClick

    End Sub
End Class